package AdminBD;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import adminHibernate.HibernateConnection;
import entidadesHibernate.Licencia;
import entidadesHibernate.Titular;
import entidadesHibernate.Vigencia;

public class DAOLicenciaMySql implements DAOLicencia {

	@SuppressWarnings("unchecked")
	public List<Licencia> getLicencias(String tipoDoc, String numDoc) {
		Session session = HibernateConnection.conectar();	
		String hql = "from Titular where tipoDoc = '" + tipoDoc + "' and dni = '" + numDoc + "'";
		
		Titular titular = new Titular();
		
		try { 
			
			session.beginTransaction();			
			Query query = session.createQuery(hql);
			titular = (Titular) query.uniqueResult();													
			session.getTransaction().commit();							
			
			}
		catch(HibernateException e) {
		       System.out.println(e);
		     } 
		finally { 
			session.close();
			}			
		
		return (List<Licencia>) titular.getLicencias();
	}

	public void guardar(Licencia licencia) {
	
		Session session = HibernateConnection.conectar();
		
		try { 
			
			session.beginTransaction();			
			session.save(licencia);										
			session.getTransaction().commit();	
			
			}
		catch(HibernateException e) {
		       System.out.println(e);
		     } 
		finally { 
			session.close();
			}		
	}

	public int obtenerUltimoNumero() {
		Session session = HibernateConnection.conectar();
		
		String hql = "select max(numero) from Licencia";
			
		Integer num=0;	
		
		try { 
			
			session.beginTransaction();	
			Query query = session.createQuery(hql);
			num = (Integer) query.uniqueResult();			
			session.getTransaction().commit();		
		
			}
		catch(HibernateException e) {
		       System.out.println(e);
		     } 
		finally { 
			session.close();
			}
		
		if(num==null) num=0;		
		
		return num;		
	}

	@SuppressWarnings("unchecked")
	public boolean existeLicencia(Titular titular) {
		Session session = HibernateConnection.conectar();	
		String hql = "from Licencia as l inner join l.titular as t where t.id = '"+titular.getIdTitular()+"'";
		
		List <Licencia> registros = new ArrayList<Licencia>();
		
		try { 
			
			session.beginTransaction();			
			Query query = session.createQuery(hql);
			registros = query.list();													
			session.getTransaction().commit();		
			
		}
		catch(HibernateException e) {
		       System.out.println(e);
		} 
		finally { 
			session.close();
		}
		
		return !registros.isEmpty();
	}

	@SuppressWarnings("unchecked")
	public List<Licencia> buscar(Titular titular, String tipo) {
		Session session = HibernateConnection.conectar();
		String hql = "select l from Licencia as l inner join l.titular as t inner join l.clase as c where t.id = '"+titular.getIdTitular()+"' and c.tipo = '"+tipo+"'";
		
		List <Licencia> registros = new ArrayList<Licencia>();
		
		try { 
			
			session.beginTransaction();			
			Query query = session.createQuery(hql);
			registros = query.list();													
			session.getTransaction().commit();		
			
		}
		catch(HibernateException e) {
		       System.out.println(e);
		} 
		finally { 
			session.close();
		}
		
		return registros;
	}

	@SuppressWarnings("unchecked")
	public boolean existe(Titular titular, String tipo) {
		Session session = HibernateConnection.conectar();
		String hql = "from Licencia as l inner join l.titular as t inner join l.clase as c where t.id = '"+titular.getIdTitular()+"' and c.tipo = '"+tipo+"'";
		
		List <Licencia> registros = new ArrayList<Licencia>();
		
		try { 
			
			session.beginTransaction();			
			Query query = session.createQuery(hql);
			registros = query.list();													
			session.getTransaction().commit();		
			
		}
		catch(HibernateException e) {
		       System.out.println(e);
		} 
		finally { 
			session.close();
		}
		
		return !registros.isEmpty();
	}

	public Licencia getLicenciaPorNum(int numero) {
		Session session = HibernateConnection.conectar();	
		String hql = "from Licencia where numero = "+numero;
		
		Licencia licencia = new Licencia();
		
		try { 
			
			session.beginTransaction();			
			Query query = session.createQuery(hql);
			licencia = (Licencia) query.uniqueResult();													
			session.getTransaction().commit();							
			
			}
		catch(HibernateException e) {
		       System.out.println(e);
		     } 
		finally { 
			session.close();
			}			
		
		return licencia;
	}

}
